from django.utils.translation import ugettext_lazy as _
from django.contrib.gis.db import models
from duetopia.register.models import DataSet
from duetopia.opensearch import OpenSearch
from duetopia import rdf
from duetopia.rdf import dct, dclite4g
import RDF

class Product(object):
	products = {}
	@classmethod
	def register_product(cls, name, product):
		cls.products[name] = product
	@classmethod
	def rdf_get_or_create(self, model, resource, *av, **kw):
		s = RDF.Statement(resource, dclite4g.fileFormat.uri, None)
		for r in model.find_statements(s):
			print "format", r.object
			format = r.object.literal_value["string"]
			if format in self.model.products:
				impl = self.model.products[format]
				return impl.objects.rdf_get_or_create(model, resource,
								      *av, **kw)
Product.model = Product
Product.objects = Product
Product._default_manager = Product

envisatPrefix = "http://catalog.terradue.com/schemas/envisat.rdf#"
acquisitionStation = rdf.parser.String(RDF.Uri(envisatPrefix + "acquisitionStation"))
softwareVersion = rdf.parser.String(RDF.Uri(envisatPrefix + "softwareVersion"))
procCentre = rdf.parser.String(RDF.Uri(envisatPrefix + "procCentre"))

class MERIS(DataSet, Product):
	class Meta:
		verbose_name = _("MERIS Product")
		verbose_name_plural = _("MERIS Products")
	class RDF(rdf.Schema):
		name = "annotation"
		properties = DataSet._meta.rdf.properties + (
			("acquisition_station", acquisitionStation),
			("processing_centre", procCentre),
			("software_version", softwareVersion),
			("region", dct.spatial),
			)
	class Search(OpenSearch):
		ShortName = _("ENVISAT MERIS")
		Description = _("Search data from the " +
				"Medium Resolution Imaging Spectrometer Instrument" +
				"For more detail about MERIS see the " +
				"<a href=\"http://envisat.esa.int/instruments/meris/\">" +
				"ESA web site</a>")
		namespaces = {
			"geo" : "http://a9.com/-/opensearch/extensions/geo/1.0/",
			"time" : "http://a9.com/-/opensearch/extensions/time/1.0/",
			}
		search_terms = ({ "name" : "keyword",
				  "type" : "rdf",
				  "uri" : rdf.dc.subject.uri,
				  "query" : "searchTerms?" },
				{ "name" : "title",
				  "type" : "django",
				  "filter" : "title__icontains",
				  "query" : "searchTerms?" },
				{ "name" : "acquisitionStation",
				  "type" : "django",
				  "filter" : "acquisition_station",
				  "query" : "searchTerms?" },
				{ "name" : "processingCentre",
				  "type" : "django",
				  "filter": "processing_centre",
				  "query" : "searchTerms?" },
				{ "name" : "start",
				  "type" : "django",
				  "filter" : "start__gte",
				  "query" : "time:Start?" },
				{ "name" : "end",
				  "type" : "django",
				  "filter" : "end__lt",
				  "query" : "time:End?" },
				{ "name" : "geometry",
				  "type" : "geodjango",
				  "filter" : "region__intersects",
				  "query" : "geo:polygon" },
				)
		display_fields = ("title", "start", "end",
				  "acquisition_station",
				  "processing_centre")
		geometry_field = "region"

	### core fields
	region = models.MultiPolygonField()
	### envisat fields
	acquisition_station = models.CharField(max_length=16)
	processing_centre = models.CharField(max_length=16)
	software_version = models.CharField(max_length=32)
		
	objects = rdf.Manager()

Product.register_product("MER_RR__1P", MERIS)
Product.register_product("MER_RR__2P", MERIS)
